package com.fmatos.amazingwallpapers.image.database;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteConstraintException;
import android.util.Log;

import com.fmatos.amazingwallpapers.image.database.dto.AlbumRow;


@Deprecated
public class AlbumsDataSource extends DataSource {

	private static final String TAG = AlbumsDataSource.class.getSimpleName();

	public AlbumsDataSource(Context context) {
		super(context,new AlbumsDatabaseOpenHelper(context) );
	}


	private AlbumRow cursorToAlbum(Cursor cursor) {
		AlbumRow row = new AlbumRow();
		
		row.setName(cursor.getString(0));
		row.setUrl(cursor.getString(1));
		row.setNiceName(cursor.getString(2));
		
		row.setUpdateTime(new DateTime(cursor.getLong(3)));
		row.setFavorite(cursor.getInt(4) == 1);
		row.setAutoUpdate(cursor.getInt(5) == 1);
		

//		Log.d(TAG,"Cursor got image " + row.getId() + " with url " + row.getUrl() + " and caption " + row.getCaption());
		return row;
	}
	

	/**
	 * Creates row for album when it doesn't already exist. In case it exists, don't update any of the fields 
	 * @param rowAlbum
	 */
	public void createAlbumRow(AlbumRow rowAlbum) {
		
		if ( ! isOpen() || rowAlbum == null ) {
			return;
		}
		
		if ( isRowExists(rowAlbum.getName()) ) {
			Log.v(TAG,"Row was found for name " + rowAlbum.getName() + ", skipping insert");
			return;
		}
		
		ContentValues values = new ContentValues();
		
		values.put(AlbumsDatabaseOpenHelper.COLUMN_NAME,rowAlbum.getName());	
		values.put(AlbumsDatabaseOpenHelper.COLUMN_URL,rowAlbum.getUrl());
		values.put(AlbumsDatabaseOpenHelper.COLUMN_NICE_NAME, rowAlbum.getNiceName());		
		
		try { 
			long insertId = getDatabase().insertOrThrow(AlbumsDatabaseOpenHelper.TABLE_ALBUMS, 
					null, values);
			if ( insertId != -1 ) {
				Log.i(TAG,"Tried insert on album (" + rowAlbum.getNiceName() + ") = " + rowAlbum.getName());
			}		

		} catch ( SQLiteConstraintException e ) {
			Log.d(TAG,"Failed inserting album inserted (" + rowAlbum.getNiceName() + ")");
		}
		
	}


	private boolean isRowExists(String albumName) {
		
		long nRows = DatabaseUtils.queryNumEntries(getDatabase(), AlbumsDatabaseOpenHelper.TABLE_ALBUMS,
				buildWhereClause(AlbumsDatabaseOpenHelper.COLUMN_NAME, albumName) );
		
		return nRows > 0;
	}


	/**
	 * Get all albums 
	 * @return
	 */
	public List<AlbumRow> getAllAlbums() {
		List<AlbumRow> albumList = new ArrayList<AlbumRow>();

		Cursor cursor = getDatabase().query(AlbumsDatabaseOpenHelper.TABLE_ALBUMS,
		    AlbumsDatabaseOpenHelper.ALL_COLUMNS, null, null, null, null, AlbumsDatabaseOpenHelper.COLUMN_NAME);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			AlbumRow comment = cursorToAlbum(cursor);
			albumList.add(comment);
			cursor.moveToNext();
		}
		// make sure to close the cursor
		cursor.close();
		
		Log.i(TAG,"Got " + albumList.size() + " albums from DB " + AlbumsDatabaseOpenHelper.TABLE_ALBUMS );
		return albumList;
	}
	
	
	public void updateAlbumTimestamp(String albumName,long timestamp) {
		Log.d(TAG,"Update album " + albumName + "  --> " + timestamp);
		
	}


	public AlbumRow findAlbum(String albumName) {

		if (! getDatabase().isOpen() && albumName == null || albumName.length() == 0) {
			return null;
		}

		String select = buildWhereClause(AlbumsDatabaseOpenHelper.COLUMN_NAME,albumName) ; 

		Cursor cursor = getDatabase().query(AlbumsDatabaseOpenHelper.TABLE_ALBUMS,
				AlbumsDatabaseOpenHelper.ALL_COLUMNS, select, null, null, null, null);

		AlbumRow albumRow = null;
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			albumRow = cursorToAlbum(cursor);
		}	

		// make sure to close the cursor
		cursor.close();
		return albumRow;

	}




}